<template>
  <div class="name-results" v-if="generatedNames.length > 0">
    <h2 class="results-title">为您推荐的名字</h2>
    
    <div class="names-grid">
      <div 
        v-for="(name, index) in generatedNames" 
        :key="name.fullName"
        class="name-card"
        :class="{ 'top-rated': index === 0 }"
      >
        <!-- 排名标识 -->
        <div class="rank-badge" v-if="index < 3">
          {{ index + 1 }}
        </div>
        
        <!-- 名字主体 -->
        <div class="name-main">
          <h3 class="full-name">{{ name.fullName }}</h3>
          <p class="meaning">{{ name.meaning }}</p>
        </div>
        
        <!-- 详细信息 -->
        <div class="name-details">
          <div class="detail-item">
            <span class="detail-label">五行：</span>
            <span class="detail-value wuxing">{{ name.wuxing }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">生肖：</span>
            <span class="detail-value zodiac">{{ name.zodiac }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">评分：</span>
            <span class="detail-value score">{{ name.score }}分</span>
          </div>
        </div>
        
        <!-- 分析说明 -->
        <div class="analysis">
          <p>{{ name.analysis }}</p>
        </div>
        
        <!-- 操作按钮 -->
        <div class="actions">
          <button 
            @click="saveName(name)"
            class="action-btn save-btn"
            :disabled="isNameSaved(name)"
          >
            <span v-if="isNameSaved(name)">已保存</span>
            <span v-else>保存</span>
          </button>
          <button 
            @click="regenerateNames"
            class="action-btn regenerate-btn"
            :disabled="isLoading"
          >
            重新生成
          </button>
        </div>
      </div>
    </div>
    
    <!-- 重新生成所有名字 -->
    <div class="regenerate-all">
      <button 
        @click="regenerateAllNames"
        class="regenerate-all-btn"
        :disabled="isLoading"
      >
        <span v-if="isLoading" class="loading">生成中...</span>
        <span v-else>重新生成所有名字</span>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useNameStore } from '@/stores/nameStore';
import type { GeneratedName } from '@/utils/nameGenerator';

const nameStore = useNameStore();

// 计算属性
const generatedNames = computed(() => nameStore.generatedNames);
const savedNames = computed(() => nameStore.savedNames);
const isLoading = computed(() => nameStore.isLoading);

// 检查名字是否已保存
const isNameSaved = (name: GeneratedName) => {
  return savedNames.value.some(saved => saved.fullName === name.fullName);
};

// 保存名字
const saveName = (name: GeneratedName) => {
  nameStore.saveName(name);
};

// 重新生成单个名字（这里简化为重新生成所有）
const regenerateNames = async () => {
  await nameStore.generateNameSuggestions();
};

// 重新生成所有名字
const regenerateAllNames = async () => {
  await nameStore.generateNameSuggestions();
};
</script>

<style scoped>
.name-results {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.results-title {
  text-align: center;
  color: #333;
  margin-bottom: 2rem;
  font-size: 1.8rem;
  font-weight: 600;
}

.names-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 2rem;
  margin-bottom: 2rem;
}

.name-card {
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  position: relative;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.name-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.name-card.top-rated {
  border-color: #fbbf24;
  background: linear-gradient(135deg, #fef3c7, #fde68a);
}

.rank-badge {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 30px;
  height: 30px;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 0.9rem;
}

.name-main {
  text-align: center;
  margin-bottom: 1.5rem;
}

.full-name {
  font-size: 2.5rem;
  font-weight: bold;
  color: #1f2937;
  margin: 0 0 0.5rem 0;
  letter-spacing: 0.1em;
}

.meaning {
  font-size: 1.1rem;
  color: #6b7280;
  margin: 0;
  font-style: italic;
}

.name-details {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding: 1rem;
  background: #f9fafb;
  border-radius: 8px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.detail-label {
  font-size: 0.8rem;
  color: #6b7280;
  margin-bottom: 0.25rem;
}

.detail-value {
  font-weight: 600;
  font-size: 1rem;
}

.wuxing {
  color: #059669;
}

.zodiac {
  color: #dc2626;
}

.score {
  color: #7c3aed;
}

.analysis {
  margin-bottom: 1.5rem;
  padding: 1rem;
  background: #f3f4f6;
  border-radius: 8px;
  border-left: 4px solid #4f46e5;
}

.analysis p {
  margin: 0;
  color: #374151;
  line-height: 1.6;
  font-size: 0.9rem;
}

.actions {
  display: flex;
  gap: 1rem;
}

.action-btn {
  flex: 1;
  padding: 0.75rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.save-btn {
  background: linear-gradient(135deg, #10b981, #059669);
  color: white;
}

.save-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

.save-btn:disabled {
  background: #9ca3af;
  cursor: not-allowed;
  transform: none;
}

.regenerate-btn {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: white;
}

.regenerate-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
}

.regenerate-btn:disabled {
  background: #9ca3af;
  cursor: not-allowed;
  transform: none;
}

.regenerate-all {
  text-align: center;
  margin-top: 2rem;
}

.regenerate-all-btn {
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: white;
  border: none;
  padding: 1rem 2rem;
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.regenerate-all-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
}

.regenerate-all-btn:disabled {
  background: #9ca3af;
  cursor: not-allowed;
  transform: none;
}

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.loading::after {
  content: '';
  width: 1rem;
  height: 1rem;
  border: 2px solid transparent;
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 768px) {
  .names-grid {
    grid-template-columns: 1fr;
  }
  
  .name-details {
    grid-template-columns: 1fr;
    gap: 0.5rem;
  }
  
  .actions {
    flex-direction: column;
  }
}
</style> 